def self.extract_variables(template)
  template.scan(/{{[^}]*}}/).map { |v| v.gsub(/{{([^}]*)}}/, '\1') }
          .map(&:to_sym)
end

def self.expand_variables(template, var_data, extra_data = {})
  data = if var_data.class <= Hash
           var_data.merge(extra_data)
         else
           resource_to_hash(var_data).merge(extra_data)
         end
  extract_variables(template).each do |v|
    unless data.key?(v)
      raise "Missing variable :#{v} in #{data} on #{caller.join("\n")}}"
    end
    template.gsub!(/{{#{v}}}/, CGI.escape(data[v].to_s))
  end
  template
end
